
package com.ikkong.shine.tools;

import java.sql.SQLException;
import java.util.List;

import com.ikkong.common.config.MainConfig;
import com.ikkong.core.dao.Blade;
import com.ikkong.core.toolbox.Record;
import com.ikkong.shine.model.Patient_queue;
import com.ikkong.shine.service.DataConnectionService;
import com.ikkong.shine.service.DbSqlService;
import com.ikkong.shine.service.impl.DataConnectionServiceImpl;
import com.ikkong.shine.service.impl.DbSqlServiceImpl;
import com.jfinal.kit.StrKit;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.druid.DruidPlugin;

public class GetDataSouceConnPatient {
	static DataConnectionService dataconnectionservice = new DataConnectionServiceImpl();
	DbSqlService dbsqlservice = new DbSqlServiceImpl();
	Log log = Log.getLog(GetDataSouceConn.class);
	Exception exception = new SQLException();
	Exception e = new Exception();
	private static String sqltopatientqueue = null;// 已分患者
	//private static String sqltopatientqueuedata = null;// 未分患者
	//private static String sqltopatientqueueitem = null;// 检查项
	
	// 获取患者信息
		public void getPatientQueue() {
			// 查询数据源连接
			Record record = dataconnectionservice.dataConnection();
			if (null == record) {
				return;
			}
			String id = record.getStr("id");
			DruidPlugin druidPlugin = null;
			ActiveRecordPlugin arp = null;
			if (!MainConfig.dbconfigs.contains(id)) {
				String driver = record.getStr("driver");
				String url = record.getStr("url");
				String username = record.getStr("username");
				String password = record.getStr("password");

				
				druidPlugin = new DruidPlugin(url, username, password.trim(),
						driver);
				arp = new ActiveRecordPlugin(id, druidPlugin);
				druidPlugin.start();
				arp.start();

				MainConfig.dbconfigs.add(id);

				List<Record> list = dbsqlservice.findDbSql();// 查询数据项信息dbsql表
				for (Record rd : list) {
					switch (rd.getInt("type")) {
					case 4:
						sqltopatientqueue = rd.getStr("sqlstring");// 已分患者信息
						break;
					/*case 5:
						sqltopatientqueuedata = rd.getStr("sqlstring");// 未分患者信息
						break;
					case 6: sqltopatientqueueitem=rd.getStr("sqlstring");//检查项信息
					  break;*/
					default:
						log.error(e.getMessage());
						break;
					}
				}
			}
			// 未分患者sqltopatientqueuedata
			// 已分患者
			if (StrKit.notBlank(sqltopatientqueue)) {
				List<com.jfinal.plugin.activerecord.Record> recordList = null;
				recordList = Db.use(id).find(sqltopatientqueue);
				long sumCount = 0;
				long suCount = 0;
				if (null != recordList) {
					sumCount = recordList.size();
					for (com.jfinal.plugin.activerecord.Record record2 : recordList) {
						Patient_queue patientqueueu = new Patient_queue();
						try {
							patientqueueu.setPatient_id(record2
									.getStr("patient_id"));
							patientqueueu.setPatient_name(record2
									.getStr("patient_name"));
							patientqueueu.setQueue_type_source_id(record2
									.getStr("queue_type_source_id"));
							patientqueueu.setRegister_id(record2
									.getStr("register_id"));
							patientqueueu.setQueue_num(record2
									.getStr("queue_num"));
							patientqueueu.setSub_queue_order(record2
									.getInt("sub_queue_order"));
							patientqueueu.setSub_queue_type(record2
									.getStr("sub_queue_type"));
							patientqueueu.setDoctor_id(record2.getInt("doctor_id"));
							patientqueueu.setFre_date(record2.getDate("fre_date"));
							patientqueueu.setContent(record2.getStr("content"));
							patientqueueu.setTime_interval(record2
									.getInt("time_interval"));
							Blade.create(Patient_queue.class).save(patientqueueu);
							suCount++;
						} catch (Exception e) {
						}
						log.error("出错数据：" + patientqueueu.toString());
					}

					log.error("PatientQueue信息同步完成。总共查询到：" + sumCount + "条数据。成功插入："
							+ suCount + "条");
				}
			}
			arp.stop();
			druidPlugin.stop();
		}
}
